# 封装断言
from utils.log_manager import my_logger
from utils.sql_manager import sql_manager
from utils.image_recognition_manager import img_manager
from drivers.driver_manager import dm
from time import sleep


class AssertManager:

    def assert_test(self, value='', when_failed=None):
        sleep(1)
        try:
            assert value in dm.dr.page_source
        except Exception as e:
            if when_failed:
                my_logger.logger.error(f'{when_failed}(页面上没有发现文本“{value}”）')
            else:
                my_logger.logger.error(f'页面上没有发现{value}')
            raise e

    def assert_sql(self, value='', target_value='*', table='', column='', limit=''):
        my_logger.logger.info('开始断言数据库里的内容')
        result = sql_manager.get_data_from_table(target_value, table, column, limit)
        try:
            assert value in str(result)
        except Exception as e:
            my_logger.logger.error(f'数据库{table}中，没有发现{value} 信息')
            raise e

    def assert_img(self, file_name=''):
        result_pic_name = '../data/result_pics/' + file_name + '.png'
        target_pic_name = '../data/target_pics/' + file_name + '.png'

        result = img_manager.campare_pic(result_pic_name, target_pic_name)
        try:
            assert result
        except Exception as e:
            my_logger.logger.error('图像比对失败！')
            raise e

    def assert_loc(self, loc, waite_time=0):
        if loc:
            try:
                dm.dr.implicitly_wait(waite_time)
                dm.dr.find_element(*loc)
            except Exception as e:
                my_logger.logger.error(f'没有找到该元素{str(loc)}')


assert_mng = AssertManager()
